package com.gzhryc.shared_device.cdcz.core.protocol.v1;

import com.gzhryc.common.IDTools;
import com.gzhryc.common.NumberTools;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.shared_device.cdcz.core.ICDCZNetworkService;
import com.gzhryc.shared_device.cdcz.core.models.CDCZChargeCommand;
import com.gzhryc.shared_device.cdcz.core.models.CDCZExecuteResult;
import com.gzhryc.shared_device.cdcz.core.protocol.v1.models.BKVData;
import com.gzhryc.shared_device.iot.core.IOTClient;

public class V1_CDCZNetworkService implements ICDCZNetworkService {

    static Logger log = Logger.getLogger(V1_CDCZNetworkService.class);

    private final IOTClient iotClient;

    public V1_CDCZNetworkService(IOTClient iotClient){
        this.iotClient = iotClient;
    }

    @Override
    public CDCZExecuteResult charge(CDCZChargeCommand chargeCommand) {
        CDCZExecuteResult result = new CDCZExecuteResult(CDCZExecuteResult.FAIL);
        try {
            V1_CDCZ_IOTCloudMessage cloudMessage = new V1_CDCZ_IOTCloudMessage();
            cloudMessage.setCmd(V1_CDCZConstants.CMD_CONTROL);
            cloudMessage.setMessageId(chargeCommand.getMessageId());
            cloudMessage.getBkvDataList().add(new BKVData(V1_CDCZConstants.KEY_PLUG_NUM,chargeCommand.getSlotNum()));
            cloudMessage.getBkvDataList().add(new BKVData(V1_CDCZConstants.KEY_CHARGE_CONTROL,1));

            Double chargeElectricity = chargeCommand.getChargeElectricity();
            Integer chargeElectricityInt = NumberTools.multiply(chargeElectricity,1000);
            cloudMessage.getBkvDataList().add(new BKVData(V1_CDCZConstants.KEY_CHARGE_MODE, chargeCommand.getCostMode()));
            cloudMessage.getBkvDataList().add(new BKVData(V1_CDCZConstants.KEY_SET_CHARGE_TIME, chargeCommand.getChargeTimes()));
            cloudMessage.getBkvDataList().add(new BKVData(V1_CDCZConstants.KEY_SET_CHARGE_ELECTRICITY, chargeElectricityInt));
            cloudMessage.setDeviceSn(chargeCommand.getDeviceSn());
            cloudMessage.setTopic("/" + chargeCommand.getDeviceSn() + "/user/get");
            if (iotClient.send(cloudMessage)) {
                result.setCode(CDCZExecuteResult.SUCCESS);
                result.setMessageId(cloudMessage.getMessageId());
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Override
    public CDCZExecuteResult closeCharge(String deviceSn, Integer slotNum) {
        CDCZExecuteResult result = new CDCZExecuteResult(CDCZExecuteResult.FAIL);
        try {
            String msgId = V1_CDCZConstants.getMessageId();

            V1_CDCZ_IOTCloudMessage cloudMessage = new V1_CDCZ_IOTCloudMessage();
            cloudMessage.setCmd(V1_CDCZConstants.CMD_CONTROL);
            cloudMessage.setMessageId(msgId);
            cloudMessage.getBkvDataList().add(new BKVData(V1_CDCZConstants.KEY_PLUG_NUM, slotNum));
            cloudMessage.getBkvDataList().add(new BKVData(V1_CDCZConstants.KEY_CHARGE_CONTROL, 0));
            cloudMessage.setDeviceSn(deviceSn);
            cloudMessage.setTopic("/" + deviceSn + "/user/get");
            if (iotClient.send(cloudMessage)) {
                result.setCode(CDCZExecuteResult.SUCCESS);
                result.setMessageId(cloudMessage.getMessageId());
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Override
    public CDCZExecuteResult searchDevice(String deviceSn) {
        CDCZExecuteResult result = new CDCZExecuteResult(CDCZExecuteResult.FAIL);
        try {
            String msgId = Long.toString(Long.valueOf(IDTools.getTimestampID()),16);

            V1_CDCZ_IOTCloudMessage cloudMessage = new V1_CDCZ_IOTCloudMessage();
            cloudMessage.setCmd(V1_CDCZConstants.CMD_SEARCH_SLOT);
            cloudMessage.setMessageId(msgId);
            cloudMessage.getBkvDataList().add(new BKVData(V1_CDCZConstants.KEY_PLUG_NUM, 0));
            cloudMessage.setDeviceSn(deviceSn);
            cloudMessage.setTopic("/" + deviceSn + "/user/get");
            if (iotClient.send(cloudMessage)) {
                result.setCode(CDCZExecuteResult.SUCCESS);
                result.setMessageId(cloudMessage.getMessageId());
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Override
    public CDCZExecuteResult restart(String deviceSn) {
        CDCZExecuteResult result = new CDCZExecuteResult(CDCZExecuteResult.FAIL);
        try {
            String msgId = Long.toString(Long.valueOf(IDTools.getTimestampID()),16);

            V1_CDCZ_IOTCloudMessage cloudMessage = new V1_CDCZ_IOTCloudMessage();
            cloudMessage.setCmd(V1_CDCZConstants.CMD_RESTART);
            cloudMessage.setMessageId(msgId);
            cloudMessage.getBkvDataList().add(new BKVData(V1_CDCZConstants.KEY_RESTART_COUNTDOWN, 0));
            cloudMessage.setDeviceSn(deviceSn);
            cloudMessage.setTopic("/" + deviceSn + "/user/get");
            if (iotClient.send(cloudMessage)) {
                result.setCode(CDCZExecuteResult.SUCCESS);
                result.setMessageId(cloudMessage.getMessageId());
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Override
    public CDCZExecuteResult findSlaveDevice(String mainDevice, Integer bindIndex) {
        return null;
    }

    @Override
    public CDCZExecuteResult bindSlaveDevice(String mainDevice, Integer bindIndex, String deviceSn) {
        return null;
    }

    @Override
    public CDCZExecuteResult removeSlaveDevice(String mainDevice) {
        return null;
    }

    public CDCZExecuteResult changeMQTTConnect(String deviceSn,String type,String ip,String port,String userName,String userPwd){
        return null;
    }

    @Override
    public CDCZExecuteResult upgradeDownload(String deviceSn, String packagePath, String packageCRC) {
        CDCZExecuteResult result = new CDCZExecuteResult(CDCZExecuteResult.FAIL);
        try {
            String msgId = Long.toString(Long.valueOf(IDTools.getTimestampID()),16);

            V1_CDCZ_IOTCloudMessage cloudMessage = new V1_CDCZ_IOTCloudMessage();
            cloudMessage.setCmd(V1_CDCZConstants.CMD_FIRMWARE_DOWNLOAD);
            cloudMessage.setMessageId(msgId);
            cloudMessage.getBkvDataList().add(new BKVData(V1_CDCZConstants.KEY_PACKAGE_PATH, packagePath,V1_CDCZConstants.VALUE_TYPE_STRING));
            cloudMessage.getBkvDataList().add(new BKVData(V1_CDCZConstants.KEY_PACKAGE_CRC, packageCRC,V1_CDCZConstants.VALUE_TYPE_STRING));
            cloudMessage.setDeviceSn(deviceSn);
            cloudMessage.setTopic("/" + deviceSn + "/user/get");
            if (iotClient.send(cloudMessage)) {
                result.setCode(CDCZExecuteResult.SUCCESS);
                result.setMessageId(cloudMessage.getMessageId());
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Override
    public CDCZExecuteResult upgrade(String deviceSn, String packagePath, String packageCRC) {
        CDCZExecuteResult result = new CDCZExecuteResult(CDCZExecuteResult.FAIL);
        try {
            String msgId = Long.toString(Long.valueOf(IDTools.getTimestampID()),16);

            V1_CDCZ_IOTCloudMessage cloudMessage = new V1_CDCZ_IOTCloudMessage();
            cloudMessage.setCmd(V1_CDCZConstants.CMD_FIRMWARE_UP);
            cloudMessage.setMessageId(msgId);
            cloudMessage.getBkvDataList().add(new BKVData(V1_CDCZConstants.KEY_PACKAGE_PATH, packagePath,V1_CDCZConstants.VALUE_TYPE_STRING));
            cloudMessage.getBkvDataList().add(new BKVData(V1_CDCZConstants.KEY_PACKAGE_CRC, packageCRC,V1_CDCZConstants.VALUE_TYPE_STRING));
            cloudMessage.setDeviceSn(deviceSn);
            cloudMessage.setTopic("/" + deviceSn + "/user/get");
            if (iotClient.send(cloudMessage)) {
                result.setCode(CDCZExecuteResult.SUCCESS);
                result.setMessageId(cloudMessage.getMessageId());
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
            result.setMsg(e.getMessage());
        }
        return result;
    }
}
